package com.ht.medicaltreatment.view

import android.content.Context
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.utils.SoundUtils
import kotlinx.android.synthetic.main.dialog_username_layout.btnNegative
import kotlinx.android.synthetic.main.dialog_username_layout.btnPositive
import kotlinx.android.synthetic.main.dialog_username_layout.etPassword
import kotlinx.android.synthetic.main.dialog_username_layout.etUserName
import kotlinx.android.synthetic.main.dialog_username_layout.imgScall
import kotlinx.android.synthetic.main.dialog_username_layout.tvSwitch

class UserNameDialog(context: Context):BaseDialog(context) {
    private var isScall = false
    override fun getLayoutRes() = R.layout.dialog_username_layout
    init {
        ScanUtils.init()

        setOnDismissListener {
            ScanUtils.close()
            btnNegative.visibility = View.VISIBLE
            btnPositive.visibility = View.VISIBLE
            etUserName.visibility = View.VISIBLE
            etPassword.visibility = View.VISIBLE
            imgScall.visibility = View.GONE
            tvSwitch.text = "扫描胸卡"
            isScall = false
        }
    }

    fun show(onPositve:(String,String?)->Unit) {
        super.show()
        etUserName.setText("")
        etPassword.setText("")
        btnNegative.setOnClickListener { dismiss() }
        btnPositive.setOnClickListener {
            val account = etUserName.text.toString()
            val psw = etPassword.text.toString()
            if(account.isEmpty() || psw.isEmpty()){
                ToastUtils.showShort("请输入账号和密码")
                return@setOnClickListener
            }
            onPositve(account,psw)
            dismiss()
        }

        //切换方式
        tvSwitch.setOnClickListener {
            if(isScall){
                tvSwitch.text = "扫描胸卡"
                btnNegative.visibility = View.VISIBLE
                btnPositive.visibility = View.VISIBLE
                etUserName.visibility = View.VISIBLE
                etPassword.visibility = View.VISIBLE
                imgScall.visibility = View.GONE
                ScanUtils.close()
            }else{
                tvSwitch.text = "账号密码"
                btnNegative.visibility = View.INVISIBLE
                btnPositive.visibility = View.INVISIBLE
                etUserName.visibility = View.INVISIBLE
                etPassword.visibility = View.INVISIBLE
                imgScall.visibility = View.VISIBLE
                ScanUtils.open {
                    ScanUtils.close()
                    SoundUtils.playSound(SoundUtils.SOUND_SUCCESS)
                    onPositve(it,null)
                    dismiss()
                }
            }
            isScall = !isScall
        }
    }

}